package mylist;

import java.util.Arrays;

public class MyArrayList implements IList{

    public int[] elem;
    public int usedSize;
    // 顺序表的默认大小
    public static final int DEFAULT_SIZE = 10;

    public MyArrayList() {
        this.elem = new int[DEFAULT_SIZE];
    }

    // 遍历顺序表中的元素
    @Override
    public void display() {
        for (int i = 0; i < this.usedSize; i++) {
            System.out.println(elem[i] + " ");
        }
        System.out.println();

    }

    @Override
    public boolean isFull() {
        // 扩容
        return this.usedSize == this.elem.length;
    }

    private void checkCapacity() {
        if (isFull()) {
            this.elem = Arrays.copyOf(this.elem, this.elem.length * 2);
        }
    }

    // 检查 pos 的合法性
    private void checkPosOnAdd(int pos) throws PosIllegality{
        if (pos < 0 || pos > usedSize) {
            throw new PosIllegality("下标不合法 pos = " + pos);
        }
    }

    @Override
    public void add(int data) {
        checkCapacity();

        this.elem[usedSize] = data;
        this.usedSize++;
    }

    @Override
    public void add(int pos, int data) {
        checkPosOnAdd(pos);
        checkCapacity();

        // 1.
        for (int i = usedSize - 1; i >= pos ; i--) {
            this.elem[i+1]  = this.elem[i];
        }

        this.elem[pos] = data;

        this.usedSize++;
    }

    @Override
    public boolean contains(int toFind) {
       if( isEmpty()) {
           return false;
       };

        for (int i = 0; i < usedSize; i++) {
            if (this.elem[i] == toFind);
            return true;
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {
        if (isEmpty()) {
            return -1;
        }

        for (int i = 0; i < usedSize; i++) {
            // 如果是查找引用类型数据，一定要重写 equals 方法
            if (this.elem[i] == toFind) {
                return i;
            }
        }
        return 0;
    }

    private void checkPosOnGetAndSet(int pos) throws PosIllegality{
        if (pos < 0 || pos >= usedSize) {
            System.out.println("参数不合法");
            throw new PosIllegality("获取指定下标元素时，参数不合法 pos = "  + pos);
        }
    }
    @Override
    public int get(int pos) throws MyArrayListEmpty{
        checkPosOnGetAndSet(pos);
        if (isEmpty()) {
            throw new MyArrayListEmpty("获取指定下标元素时,顺序表为空！");
        }
        return this.elem[pos];
    }

    @Override
    public void set(int pos, int value) {
        checkPosOnGetAndSet(pos);
        if (isEmpty()) {
            throw new MyArrayListEmpty("获取指定下标元素时,顺序表为空！");
        }

        this.elem[pos] = value;

    }

    @Override
    public void remove(int toRemove) {
        int index = indexOf(toRemove);
        if (index == -1) {
            System.out.println("数字没有找到 " + toRemove );
        }
        for (int i = index; i < this.usedSize - 1; i++) {
            this.elem[i] = this.elem[i + 1];
        }

        --this.usedSize;

    }

    @Override
    public int size() {
        return this.usedSize;
    }

    @Override
    public void clear() {
        this.usedSize = 0;
    }

    @Override
    public boolean isEmpty() {
        return this.usedSize == 0;
    }
}
